import { getStream } from '../Core/connect'
import { Vector3 } from '../Math/Vector3'
import { Vector2 } from '../Math/Vector2'
import * as MathUtils from '../Math/MathUtils'

const list : Array<string>= []
export class WebUI {
    name: string
    position: Vector3
    uuid: string
    drawsize: Vector2
    Offset: Vector2
    url:string
    // listenmessagefun:Function
    constructor(option: WebUIOption) {
        this.uuid = MathUtils.generateUUID()
        this.name = option.name
        this.position = option.position ? option.position : new Vector3(0, 0, 0)
        this.Offset = option.Offset ? option.Offset : new Vector2(0, 0)
        this.url = option.url
        this.drawsize = option.drawsize
        // this.listenmessagefun = ()=>{}
        this.init()
    }
    init() {
        const stream = getStream()
        const str = { 
            WebUIfunction: 'createWebUI',
            ids: `${this.uuid}`, 
            position: `${this.position.x},${this.position.y},${this.position.z}`,
            url:`${this.url}`,
            drawsize:`${this.drawsize.x},${this.drawsize.y}`
        }
        console.log(str)
        stream.emitUIInteraction(str)
    }


    /* webui功能型部分
        webUI与推流页面交互
    
    */
    //设置ui位置
    setPosition(position: Vector3) {
        this.position = position
        const stream = getStream()
        const str = {WebUIfunction: 'setPosition', ids: `${this.uuid}`, position: `${this.position.x},${this.position.y},${this.position.z}`}
        stream.emitUIInteraction(str)
    }
    //设置ui绘制大小
    setDrawSize(drawsize: Vector2) {
        this.drawsize = drawsize
        const stream = getStream()
        const str = {WebUIfunction: 'setDrawSize', ids: `${this.uuid}`, drawsize: `${this.drawsize.x},${this.drawsize.y}`}
        stream.emitUIInteraction(str)
    }
    //设置ui偏移
    setUIOffset(Offset: Vector2) {
        this.Offset = Offset
        const stream = getStream()
        const str = {WebUIfunction: 'setUIOffset', ids: `${this.uuid}`, Offset: `${this.Offset.x},${this.Offset.y}`}
        stream.emitUIInteraction(str)
    }

    //开启/关闭前端消息监听
    startListenMessage(panduanci:string,fun: Function){
        const stream = getStream()
        list.push(panduanci)
        const getUIMessage = (data: string) => {
            const returnData = JSON.parse(data)
            console.log(returnData)
             if(returnData.uuid === this.uuid&&list.includes(returnData.WebUIcallbackfunction)){
                //确定是相对应的回话
                fun(returnData.WebUIcallbackfunction,returnData)
            }
        }
        stream.addResponseEventListener("getUIMessage"+this.uuid, getUIMessage)
        // this.listenmessagefun = getUIMessage
    }
    stopListenMessage(){
        const stream = getStream()
        stream.removeResponseEventListener("getUIMessage"+this.uuid)
    }

    //删除ui
    delete() {
        const stream = getStream()
        const str = {WebUIfunction: 'deleteWebUI', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
	}

}